// pages/index.js
import store from '../../modules/store'
import tool from '../../modules/tool'
import config from '../../config'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loaded: false,
    currentPageIndex: 0,
    imagePrefix: config.imagePrefix,
    cartNum: 0,
    showPayBoard: 0,
    payBoardData: {},
    showCart: false,
  },

  handleHidePayBoard() {
    this.setData({
      showPayBoard: 0
    })
  },

  handleShowCart() {
    tool.handleShowCart(this.data.showCart)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {

    console.log('输入', options)
    // wx.redirectTo({
    //   url: '/pages/printTest/printTest',
    // })
    // return
    //准备整个小程序的数据

    // return

    //获取用户openId
    {
      if (!(await tool.testConnection())) {
        tool.handleShowModel('服务异常', false)
        return
      }
      await tool.handleLogin()
      // let re = await tool.getOpenId()
      // if (!re) {
      //   wx.showModal({
      //     title: '提示',
      //     content: '获取openId失败，请尝试重启小程序。',
      //     showCancel: false
      //   })
      //   return
      // }
    }
    //更新窗口尺寸信息
    tool.updateWindowInfo()
    //更新主页当前页面序号
    tool.updateMainCurrentPageIndex(this.data.currentPageIndex)

    store.onChange('mainCurrentPageIndex', (e) => {
      let data = store.getItem('mainCurrentPageIndex')
      this.setData({
        currentPageIndex: data
      })
      if (data == 4) {
        store.setItem('navigateBarStatus', 0)
      } else {
        store.setItem('navigateBarStatus', 1)
      }
    })
    //获取轮播图
    {
      let re = await tool.requestData('/setting/home', {}, 'get')
      if (re.status) {
        store.setItem('banner', re.data.banner)
        store.setItem('newUserCoupon', re.data.newUserCoupon)
        store.setItem('notification', re.data.notification)
        store.setItem('homeProducts', re.data.products)
        store.setItem('homeActivities', re.data.activities)
        store.setItem('storeClosed', re.data.storeClosed)

        if (re.data.storeClosed) {
          tool.handleShowModel('门店已关闭', false)
        }
      }
    }
    //获取购物车
    if (store.getItem('userData') !== undefined) {
      await tool.handleUpdateCartNum()
      this.setData({
        cartNum: store.getItem('cartNum')
      })
      store.onChange('cartNum', () => {
        this.setData({
          cartNum: store.getItem('cartNum')
        })
      })
    }
    //弹出事件
    store.onChange('indexShowPayBoard', () => {
      this.setData({
        payBoardData: store.getItem('indexShowPayBoard'),
        showPayBoard: 1
      })
    })
    store.onChange('indexHidePageBoard', () => {
      this.setData({
        showPayBoard: 0
      })
    })
    store.onChange('indexHidePayBoard', () => {
      this.setData({
        showPayBoard: 0
      })
    })
    //完成准备整个小程序的数据，显示页面
    this.setData({
      ...store.getItem('windowInfo'),
      loaded: true
    })
    store.setItem('requestShowLoading', true)

    // tool.requestData('/order/getPrintLabelInfo', {
    //   orderNo: 'ORD20250117185857000001690113'
    // },'get')

    store.onChange('showCart', () => {
      this.setData({
        showCart: store.getItem('showCart')
      })
    })

    //链接
    if (options && options.type == 'share') {
      tool.handleShowProductActivityDetailPage(options.isPro == 'true', parseInt(options.id))
    }
    if (options && options.type == 'shareLink') {
      if (tool.isLogined()) {
        tool.flow(async () => {
          await tool.handleUserShareLink(options.id)
          tool.handleShowToast('新增一张优惠券', 'success')
          await tool.wait(0.5)
          tool.handleShowSelectCouponPage()
        }, err => tool.handleShowToast(`${err}`, 'error'))
      } else {
        tool.handleNotLogin()
      }
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})